package com.crb.smartcard.cardutils;

import android.content.Context;
import android.os.Build;


import com.crb.smartcard.SmartCardConfig;
import com.crb.smartcard.cardutils.entity.SimCardAndDeviceInfoEntity;
import com.crb.smartcard.core.bean.CardResult;
import com.crb.smartcard.core.oma.SmartCard;
import com.crb.smartcard.utils.LogUtil;
import com.crb.smartcard.utils.NfcUtil;
import com.crb.smartcard.utils.ReverseUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 获取Sim卡和Device信息
 */
public class SimCardAndDeviceInfoUtil {

    private static final String TAG = "SimCardAndDeviceInfoUtil";

    /**
     * 获取Sim卡和Device信息
     */
    public static SimCardAndDeviceInfoEntity getSimCardAndDeviceInfo(Context context) {
        SimCardAndDeviceInfoEntity simCardAndDeviceInfo = new SimCardAndDeviceInfoEntity();
        //获取iccid
        List<String> cApduList = new ArrayList<>();
        cApduList.add("00A4040010A0000000871002FF86FFFF89FFFFFFFF");
        cApduList.add("01A4000C023F00");
        cApduList.add("01A4000C022FE2");
        cApduList.add("00B000000A");
        List<CardResult> rApduList = new ArrayList<>();
        for (String str : cApduList) {
            CardResult execute = SmartCard.execute(str);
            rApduList.add(execute);
        }
        CardResult cardResult = rApduList.get(rApduList.size() - 1);
        //设置iccid
        if (cardResult.getStatus() == 0) {
            String iccid = ReverseUtil.reverse(cardResult.getRapdu());
            SmartCardConfig.SIM_ICCID = iccid;
            simCardAndDeviceInfo.setIccid(iccid);
        }
        //是否有NFC
        if (NfcUtil.hasNfc(context)) {
            simCardAndDeviceInfo.setHasNfc(true);
        } else {
            simCardAndDeviceInfo.setHasNfc(false);
        }
        //NFC是否可用
        if (NfcUtil.isEnableNfc(context)) {
            simCardAndDeviceInfo.setEnableNfc(true);
        } else {
            simCardAndDeviceInfo.setEnableNfc(false);
        }
        //手机号
        simCardAndDeviceInfo.setPhoneNumber(SmartCardConfig.PHONENUMBER);
        //手机型号
        simCardAndDeviceInfo.setDevice_vendor(Build.BRAND);//设备厂商代码
        simCardAndDeviceInfo.setDevice_model(Build.DEVICE);//设备型号代码
//        String s = new Gson().toJson(simCardAndDeviceInfo);
//        LogUtil.e(TAG, s);
        SmartCard.closeChannel();
//        return s;
        return simCardAndDeviceInfo;
    }
}
